class Document < RecommObject

  attr :input
  attr :words_array
  
  def initialize(input)
    @input = input
    clean_input
    generate_words_array
    remove_stop_words
    stem_words
    make_keywords_hash
  end
  
  private

  def clean_input
    
    @input.downcase!
    @input.gsub!(/(<[^>]*>)|\n|\t/s, '') # makni HTML tagove 
    @input.gsub!(/[^[:alnum:] ]/, '') # makni sve znakove osim slova brojeva i razmaka
    @input.gsub!(/ +/, ' ') # makni visestruke razmake
    
  end
  

  
  # generate list of all words
  def generate_words_array
    @words_array = @input.split(" ")
  end
  
  
  # takes stop words from a file an removes them
  def remove_stop_words
    
    stop_words = Array.new
    
    # Procitaj sve rijeci iz datoteke, odrezi \n znak i dodaj u listu
    File.open('lib/stop_words.txt').each_line{ |word| stop_words.push(word.chop) }
    
    # Makni sve rijeci s liste stop wordova
    @words_array.delete_if do |word|
      stop_words.any? { |stop_word| stop_word == word }
    end
    
  end
  
  # napravi stemove od svih rijeci
  def stem_words
    stemmer = Lingua::Stemmer.new(:language => "en")
      @words_array = @words_array.map {|word| stemmer.stem(word)}
  end
  
  # prebroji svaku rijec i napravi hash u obliku rijec => broj_ponavljanja
  def make_keywords_hash
    unique_words = @words_array.uniq
    @keywords_hash = Hash.new
    
    #prebroji svaku rijec i spremi u hash
    unique_words.each do |word|
      appearances = @words_array.find_all{|element| element == word}.size
      
      if appearances > 1
        @keywords_hash.store(word,appearances)
      end
    end
    
  end
  
  

end


